//
//  DDWelcomeJobFindersViewController.m
//  DingDongZP
//
//  Created by Jyh on 16/4/10.
//  Copyright © 2016年 iyhjiang.com. All rights reserved.
//

#import "DDWelcomeJobFindersViewController.h"
#import "DDJobfindersService.h"
#import "DDJobfindersBaseModel.h"
#import "DDDiscoverJobFinderCell.h"

@interface DDWelcomeJobFindersViewController ()

@property (strong, nonatomic) DDJobfindersService *jobfindersService;

@end

@implementation DDWelcomeJobFindersViewController

static NSString *DDDiscoverJobfinderCellIdentifier = @"DDDiscoverJobFinderCell";

#pragma mark
#pragma mark - View LifeCycle

- (instancetype)init
{
    self = [super init];
    if (self) {
        
        self.jobfindersService = [[DDJobfindersService alloc] init];
        self.startIndexPage = 0;
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setupData];
    [self setupView];
}


#pragma mark
#pragma mark - Private Methods

- (void)setupData
{
    [self requestJobsData];
}

- (void)setupView
{
    [self configureTableView];
    
}
- (void)configureTableView
{
    self.tableView.rowHeight = HEIGHT_DISCOVER_CELL;
    self.tableView.tableFooterView = [[UIView alloc] init];
    
//    [self.tableView mas_remakeConstraints:^(MASConstraintMaker *make) {
//        
//        make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
//    }];
     self.tableView.contentInset = UIEdgeInsetsMake(2, 0, 10, 0);
    UINib *cellNib = [UINib nibWithNibName:@"DDDiscoverJobFinderCell" bundle:nil];
    [self.tableView registerNib:cellNib forCellReuseIdentifier:DDDiscoverJobfinderCellIdentifier];
    
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self addRefreshHeader];
    [self addRefreshFooter];
}

- (void)loadNewData
{
    self.startIndexPage = 0;
    [self requestJobsData];
}

- (void)loadMoreData
{
    self.startIndexPage ++;
    [self requestJobsData];
}

- (void)showMenuButtonClick:(UIButton *)button
{
    
}

#pragma mark
#pragma mark - Data

/**
 *  未登录状态最多能展示15条， index从0开始
 */
- (void)requestJobsData
{
    if (self.startIndexPage == 0) {
        [self.dataSource removeAllObjects];
//        [self showLoading];
    }
    
    WEAKSELF
    [self.jobfindersService queryJobfindersWithPage:[NSString stringWithFormat:@"%d",(int)self.startIndexPage] successBlock:^(id responseObject) {
        [weakSelf hideLoading];
        if ([weakSelf isValidateResponseData:responseObject]) {
            // 请求成功
            DDJobfindersBaseModel *model = [DDJobfindersBaseModel yy_modelWithJSON:responseObject[@"data"]];
            
            [weakSelf.dataSource addObjectsFromArray:model.jobfinders];
            [weakSelf.tableView reloadData];
            
            //上拉刷新状态
            if ([weakSelf isRefreshing]) {
                [self setFooterHidden:model.jobfinders.count < self.pageCount ? YES : NO];
                
                [self endRefreshHeader];
                
            } else {
                //下拉刷新
                if ([model.jobfinders count] < self.pageCount) {
                    [self setFooterHidden:YES];
                }
                [self endRefreshFooter];
            }
            
            if (self.startIndexPage == 1) {
                // 隐藏上拉加载
                [self setFooterHidden:YES];
            }
            
        } else {
            [weakSelf responseUnValidate:responseObject];
        }
        
    } failBlock:^(id responseObject, NSError *error) {
        [weakSelf requestFaild];
    }];
}

#pragma mark -
#pragma mark UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.dataSource count];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    DDDiscoverJobFinderCell *cell = (DDDiscoverJobFinderCell *)[tableView dequeueReusableCellWithIdentifier:DDDiscoverJobfinderCellIdentifier];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    if ([self.dataSource count] > indexPath.row) {
        [cell updateViewWithData:(DDJobfinderDetail *)self.dataSource[indexPath.row]];
    }
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return  172;
}

#pragma mark -
#pragma mark - UITableViewDelegate

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    
    if (![DDLoginUserManager isLogin]) {
        
        
//        [self showLDDefaultAlertView:@"需要登录帐号" sure:^{
//            
//            [weakSelf setNoLoginRecVC];
//        } cancel:^{
//            
//        }];
//        WEAKSELF
        [[NSOperationQueue mainQueue] addOperationWithBlock:^{
            
//            [weakSelf showLDAlertView:@"需要登录帐号" sure:^{
//                
//                [weakSelf setNoLoginRecVC];
//            } cancel:nil];
            
            /**
             *  在 DDWelcomeViewController 控制器中显示登录的弹窗
             */
            [[NSNotificationCenter defaultCenter]postNotificationName:@"popView" object:nil userInfo:nil];
        }];
        
    }
}

- (void)setNoLoginRecVC
{
//    DDLoginViewController *recVC = [[DDLoginViewController alloc] init];
////    LDRootNavVC *rootNav = [[LDRootNavVC alloc] initWithRootViewController:recVC];
//    
//    [self.navigationController pushViewController:recVC animated:false];
        WEAKSELF
        [[DDLoginManager sharedManager] showLoginViewControllerFromViewController:self showCancelButton:NO success:^{
            
            [weakSelf jumpToMainVC];
            
        } failure:^(NSString *errorMessage) {
            
        } cancel:^{
            
        }];
}

- (void)jumpToMainVC
{
    AppDelegate *delegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    [delegate initCattleTabbarController];
}

@end
